/**
 * 2010(c) Copyright Oceansoft Information System Co.,LTD. All rights reserved.
 * <p>
 * Compile: JDK 1.6+
 * <p>
 * 版权所有(C)：江苏欧索软件有限公司
 * <p>
 * 公司名称：江苏欧索软件有限公司
 * <p>
 * 公司地址：中国苏州科技城青山路1号
 * <p>
 * 网址: http://www.oceansoft.com.cn
 * <p>
 * 版本: 苏州公安统一用户管理平台1.0
 * <p>
 * 作者: sunj
 * <p>
 * 文件名:SysDeptServiceImpl.java
 * <p>
 * 类产生时间: 2012-5-31 上午16:30:25
 * <p>
 * 所在组 : 苏州公安统一用户管理平台
 * <p>
 * 所在部门: 电信/国土——技术二部
 * <p>
 * <p>
 */
package com.oceansoft.core.service.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.oceansoft.core.common.util.Pagination;
import com.oceansoft.core.dao.ISysDeptDao;
import com.oceansoft.core.dao.ISysUserDao;
import com.oceansoft.core.domain.SysDept;
import com.oceansoft.core.domain.SysUser;
import com.oceansoft.core.service.ISysDeptService;

/**
 * 部门信息业务组件管理实现类
 * 
 * @author sunj
 * 
 *         2012-6-5上午09:29:03
 */
@Service
public class SysDeptServiceImpl implements ISysDeptService {

	@Resource
	private ISysDeptDao sysDeptDao;

	@Resource
	private ISysUserDao sysUserDao;

	public List<SysDept> findDeptTree(String deptId,SysUser user,String isparent) {
		/*if (!StringUtils.hasLength(deptId)) {
			// 查询所有一级部门
			//new add arg-> user
			return sysDeptDao.findAllDept(user);
		} else {*/
			return sysDeptDao.findAllDept(deptId,Integer.parseInt(isparent));
		//}
	}

	public SysDept findByDeptId(String deptId) {
		List<SysDept> list = sysDeptDao.findDeptByDeptId(deptId);
		return list.size() > 0 ? list.get(0) : new SysDept();
	}

	/********
	 * 
	 * 
	 * 
	 * 
	 * 
	 * 
	 */

	@Override
	public List<SysDept> findAllDept(String path) {
		return sysDeptDao.findAllDept("3205", path);
	}

	@Override
	public List<SysDept> findAllAsyncDept() {
		return sysDeptDao.findAllAsyncDept();
	}

	@Override
	public List<SysDept> findAllDept(SysUser user) {
		return sysDeptDao.findAllDept(user);
	}

	@Override
	public List<SysDept> findAllDept(String deptId, String path) {
		return sysDeptDao.findAllDept(deptId, path);
	}

	@Override
	public List<SysUser> findUserByDeptId(String deptId) {
		return sysUserDao.findNameIdByDept(deptId);
	}

	@Override
	public List<SysDept> findDeptByPid(String pId) {
		return sysDeptDao.findDeptByPid(pId);
	}

	@Override
	public List<SysDept> findDeptByDeptId(String deptId) {
		return sysDeptDao.findDeptByDeptId(deptId);
	}

	@Override
	public List<SysDept> findDeptById(String deptId) {
		return sysDeptDao.findDeptById(deptId);
	}

	@Override
	public Pagination<SysDept> findDeptByPagination(String deptId, int pageIndex) {
		return sysDeptDao.findDeptByPagination(deptId, pageIndex);
	}

	@Override
	public void deleteDeptByDeptId(String deptId) {
		sysDeptDao.deleteDeptByDeptId(deptId);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.oceansoft.core.service.ISysDeptService#fuzzyQuery(java.util.Map,
	 * int)
	 */
	@Override
	public Pagination<SysDept> fuzzyQuery(Map<String, String> params, int pageIndex) {
		return sysDeptDao.fuzzyQuery(params, pageIndex);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public int addDept(SysDept dept) {
		// 看部门编号是否重复
		List<SysDept> list = sysDeptDao.findDeptById(dept.getDeptId());
		if (list.size() > 0)
			return 2;
		int count = sysDeptDao.findCountByNameAlias(dept);
		if (count > 0)
			return 3;
		// 根据父部门编号获取子部门序号的最大值，在此基础上+1作为新的ordernum
		int orderNum = sysDeptDao.getOrderNumber(dept.getParentId()) + 1;
		dept.setOrderNum(orderNum);
		int result = sysDeptDao.add(dept);
		return result;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.oceansoft.core.service.ISysDeptService#saveEdit(com.oceansoft.core
	 * .domain.SysDept)
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public int saveEdit(SysDept dept) {
		List<SysDept> list = sysDeptDao.findDeptByDeptId(dept.getDeptId());
		int count = sysDeptDao.findCountByNameAlias(dept);
		if (count > 0)
			return 3;
		SysDept sysDept = sysDeptDao.findByDbId(dept.getDbId());
		if (sysDept != null) {
			if (list.size() == 1) {
				if (sysDept.getDbId() != list.get(0).getDbId())
					return 2;
			}
		}
		int result = sysDeptDao.saveEdit(dept);
		sysDeptDao.updateChildren(sysDept.getDeptId(), dept.getDeptId());
		return result;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.oceansoft.core.service.ISysDeptService#findParents(java.lang.String)
	 */
	@Override
	public List<String> findParents(String deptId) {
		return sysDeptDao.findParents(deptId);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.oceansoft.core.service.ISysDeptService#findParents(java.lang.String)
	 */
	@Override
	public List<String> findParents(String deptId, String policeNum) {
		// Manager manager = managerDao.findByPoliceNum(policeNum);
		// String manageDept = manager.getDeptId();
		// SysDept sysDept = sysDeptDao.findDeptById(manageDept).get(0);
		// return sysDeptDao.findParents(deptId,sysDept.getDeep());
		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.oceansoft.core.service.ISysDeptService#deleteDept(java.lang.String)
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public int deleteDept(String deptId) {
		int childrenCount = sysDeptDao.findChindrenCount(deptId);
		// 非叶子部门
		if (childrenCount != 0)
			return 1;
		int userCount = sysUserDao.findCountByDept(deptId);
		// 部门不为空
		if (userCount != 0) {
			return 2;
		}

		SysDept dept = sysDeptDao.findDeptById(deptId).get(0);
		sysDeptDao.deleteDeptByDeptId(deptId);
		// List<AppInfo> appinfos = appInfoDao.findAllAppInfos();
		// for(AppInfo app:appinfos){
		// if ("1".equals(app.getPushFlag()) && "1".equals(app.getActiveflag()))
		// {
		// Message msg = sysDataSyncDao.syncSysDeptData(dept,
		// app,"delete",null);
		// //日志
		// if (msg.getStatus() == Constant.RESULT_FAILD) {
		// SysSyncLog sysSyncLog = new SysSyncLog();
		// sysSyncLog.setSynctime(new Date());
		// sysSyncLog.setSynctab(Constant.TAB_SYS_DEPT);
		// sysSyncLog.setSyncres(Constant.SYNC_RESULT_FAIL);
		// sysSyncLog.setRemark(msg.getBody());
		// sysSyncLog.setAppid(app.getAppid());
		// sysSyncLogDao.insertSysSyncLog(sysSyncLog);
		// } else {
		// SysSyncLog sysSyncLog = new SysSyncLog();
		// sysSyncLog.setSynctime(new Date());
		// sysSyncLog.setSynctab(Constant.TAB_SYS_DEPT);
		// sysSyncLog.setSyncres(Constant.SYNC_RESULT_SUCCESS);
		// sysSyncLog.setRemark(msg.getBody());
		// sysSyncLog.setAppid(app.getAppid());
		// sysSyncLogDao.insertSysSyncLog(sysSyncLog);
		// }
		// }
		// }
		return 0;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveChangOrder(String deptId, String targetDeptId) {
		// 先查找出当前部门和目标部门的排序序号
		SysDept dept = sysDeptDao.findDeptById(deptId).get(0);
		SysDept target = sysDeptDao.findDeptById(targetDeptId).get(0);
		int orderNum = dept.getOrderNum();
		int targetOrder = target.getOrderNum();

		// 修改介于两者之间的排序序号
		sysDeptDao.updateOrderNums(orderNum, targetOrder, deptId);
		// 上移的时候修改目标ordernum
		if (orderNum > targetOrder)
			targetOrder++;
		// 修改目标序号
		sysDeptDao.updateOrderNum(targetOrder, deptId);
	}

	@Override
	public List<SysDept> findDeptInfoByDeptId(String deptId) {
		return sysDeptDao.findDeptInfoByDeptId(deptId);
	}

	@Override
	public Pagination<SysDept> findNameByPagination(String deptId, int pageIndex) {
		return sysDeptDao.findNameByPagination(deptId, pageIndex);
	}

	@Override
	public List<Object> findBackTree(String deptId, String policeNum, String path) {
		// Manager manager = managerDao.findByPoliceNum(policeNum);
		// List<Object> obj = new ArrayList<Object>();
		// //没有设置管理权限或者不是管理员
		// if(manager == null)
		// return obj;
		//
		// if(!StringUtils.hasLength(deptId)){
		// //第一次加载数的根节点和其子节点
		// obj.add(sysDeptDao.findAllDept(manager.getDeptId(),path));
		// obj.add(manager.getDeptId());
		// }else{
		// obj.add(sysDeptDao.findDeptByPid(deptId));
		// obj.add(manager.getDeptId());
		// }
		// return obj;
		return null;
	}

	@Override
	public List<SysDept> findContactDepts(String iconPaht) {
		return sysDeptDao.findContactDepts(iconPaht);
	}

	@Override
	public List<SysDept> findByJpin(String jpin) {
		return sysDeptDao.findByJpin(jpin);
	}

        public String findDeptByAlias(String alias){
            return sysDeptDao.findDeptByAlias(alias);
        }

		@Override
		public List<SysDept> findByDw1AndDw2(String dw1, String dw2) {
			return sysDeptDao.findByDw1AndDw2(dw1,dw2);
		}

		@Override
		public SysDept selDept(String dw1) {
			return sysDeptDao.selDeptInfo(dw1);
		}

		@Override
		public List<SysDept> findSubDeptTree(String dw1,String deptId) {
			if (!StringUtils.hasLength(deptId)) {
				// 查询所有一级部门
				return sysDeptDao.findAllSubDept(dw1);
			} else {
				return sysDeptDao.findAllDept(deptId,"0");
			}
		}

		@Override
		public List<SysDept> _findByJpin(String jpin) {
			return sysDeptDao._findByJpin(jpin);
		}

	@Override
	public int finddeptcount(String deptId) {
		return sysDeptDao.finddeptcount(deptId);
	}

	@Override
	public SysDept findByParentAndName(String deptId, String name) {
		return sysDeptDao.findByParentAndName(deptId,name);
	}
}
